<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新闻</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <script src="/js/utils.js"></script>
    <style type="text/css">
        .layui-table-cell{height:40px;line-height:40px;}
    </style>
</head>
<body>

<blockquote class="layui-elem-quote quoteBox mod_default_box">
    <form class="layui-form">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="name" placeholder="请输入新闻标题" autocomplete="off" class="layui-input" />
            </div>
            <div class="layui-input-inline">
                <label class="layui-form-label">新闻类型</label>
                <div class="layui-input-block">
                    <select lay-search id="tpid" name="tpid">
                        <option value="">请选择新闻类</option>
                    </select>
                </div>
            </div>
            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-stu-submit">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </form>
</blockquote>

<!--数据列表-->
<table class="layui-hide" id="test" lay-filter="stu-table"></table>
<script type="text/html" id="tb">
    <div class="layui-container" style="margin-left:0px;">
        <a class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-addition"></i>添加新闻</a>
        <a class="layui-btn layui-btn-xs" lay-event="lunbo"><i class="layui-icon layui-icon-picture"></i>轮播图</a>
    </div>
</script>
<script type="text/html" id="stu-toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
</script>
<!--<script type="text/html" id="checkboxTpl">-->
<!--    <input type="checkbox" name="status" value="{{d.id}}" title="启用" lay-filter="lockDemo" {{ d.status == 1 ? 'checked' : '' }} />-->
<!--</script>-->
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script>
        var $ = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var upload = layui.upload;
        table.render({
            elem: '#test'
            ,id:'stu-table-id'//重载需要
            ,url:'/news/types'
            ,toolbar: '#tb'
            ,cols: [[
                {field: 'id',title: '序号',type:'numbers',width:80,align: 'center'}
                ,{field:'nstitle',align : 'center',width : 280, title: '新闻标题'}
                // ,{field:'nssubtitle',align : 'center', width : 240,title: '新闻副标题'}
                ,{field:'nscontent',align : 'center', width : 240,title: '新闻内容'}
                ,{field:'nsimg',align : 'center', title:'新闻图片',width:150,templet: function (d) {
                        if(d.nsimg)
                            return   '<div><img src="/upload/'+d.nsimg+'" style="width:70px;height:50px;" onclick="previewImgs(this)"></div>'
                    }}
                ,{field:'tpid',align : 'center', width : 140,title: '新闻主题分类',templet: function (d) {
                                return d.newtype.name;
                    }}
                ,{field:'nsstatus',align : 'center', title:'是否显示',width:110
                    ,templet:function (d) {
                        if (d.nsstatus == 0) {
                            return "否"
                        }else {
                            return "是"
                        }
                    }}
                ,{field:'showindex',align : 'center', title:'是否显示轮播图位置',width:160,templet: function(d){
                    if(d.showindex == 0){
                        return "否"
                    }else{
                        return "是"
                    }
                    }}

                ,{field:'status',align : 'center', title:'是否热门',width:110,templet: function(d){
                    if(d.status == 0){
                        return "否"
                    }else {
                        return "是"
                    }
                    }}

                ,{field:'nstime',align : 'center', width : 180,title: '新闻发表时间'}
                ,{toolbar:"#stu-toolbar",align : 'center',title:"操作",fixed: 'right',width :200}
            ]]
            ,page: true
            ,page: {
                curr: layui.data("index_page").index
            },
            done: (res, curr, count) => {
                layui.data("index_page", {
                    key: 'index',
                    value: curr
                });
            }
        });

        $.ajax({
            url: "/newtype/newtypeAll"
            , success: function (res) {
                if (res.code == 0) {
                    // 下拉列表添加数据,再渲染
                    $.each(res.data, function (index, item) {
                        $("select[name = 'tpid']").append(new Option(item.name, item.id));
                    });
                    form.render();
                } else if (res.code == 1) {
                    layer.msg(res.msg);
                } else {
                    layer.msg('拒绝访问');
                }
            }
            , error: function () {
                console.log("ajax error");
            }
        });

        //搜索操作
        form.on('submit(search-stu-submit)',function (data) {
            table.reload('stu-table-id',{
                where:{"key":data.field.name,
                        "tpid":data.field.tpid}
                ,page:{
                    curr:1
                }
            });
            return false;
        });

        //添加操作
        table.on('toolbar(stu-table)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    type: 2
                    ,title: "添加信息"
                    ,area: ['100%', '100%']
                    ,skin: 'layui-layer-molv'
                    ,content: (encodeURI("add_xinwen.html")) //这里content是一个普通的String
                });
            }else if(obj.event=="lunbo"){
                layer.open({
                    type: 2
                    ,title: "页面轮播图"
                    ,area: ['90%', '90%']
                    ,content: (encodeURI("../lunbo/lunboList.html?type=xinwen")) //这里content是一个普通的String
                });
            }
        });

        //修改操作
        table.on('tool(stu-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    ,title: "修改信息"
                    ,skin: 'layui-layer-molv'
                    ,area: ['100%', '100%']
                    ,content: (encodeURI('update_xinwen.html?nsid='+data.nsid)) //这里content是一个普通的String
                });
            }
    });
</script>
<script>
    function previewImgs(obj) {
        var img = new Image();
        img.src = obj.src;
        //var height = img.height + 50; // 原图片大小
        //var width = img.width; //原图片大小
        var imgHtml = "<img src='" + obj.src + "' width='800px' height='500px'/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            area: [800 + 'px', 550 + 'px'],// area: [width + 'px',height+'px'],  //原图显示
            shadeClose: true,
            scrollbar: false,
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
</script>
</body>
</html>